package com.wecan.saasdata.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wecan.base.PO.User;
import com.wecan.base.enums.UserStatus;
import com.wecan.saasdata.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author liuzhihao
 * @Date 2022.01.11
 * @Version 1.0.0
 * @Description 用户数据服务, 对接衍生的数据逻辑
 */
@Component
public class UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 通过姓名和密码获取用户
     *
     * @param name
     * @return
     */
    public User queryUserByNameAndPass(final String name, final String password) {
        return userMapper.<User>selectOne(Wrappers.<User>lambdaQuery().and(i -> i.eq(User::getName, name).or().eq(User::getPhone, name)).eq(User::getPassword, password));
    }

    /**
     * 通过手机号获取正常使用的用户
     *
     * @param phone
     * @return
     */
    public User queryNormalUserByPhone(final String phone) {
        return userMapper.<User>selectOne(Wrappers.<User>lambdaQuery().eq(User::getPhone, phone).eq(User::getStatus, 1));
    }

    /**
     * 通过手机号获取用户
     *
     * @param phone
     * @return
     */
    public User queryUserByPhone(final String phone) {
        return userMapper.<User>selectOne(Wrappers.<User>lambdaQuery().eq(User::getPhone, phone));
    }

    /**
     * 通过用户id获取用户
     *
     * @param id
     * @return
     */
    public User queryUserById(final String id) {
        return userMapper.<User>selectOne(Wrappers.<User>lambdaQuery().eq(User::getId, id));
    }

    /**
     * 根据手机号修改密码
     *
     * @param phone
     * @param password
     * @return
     */
    public boolean editPasswordbyPhone(final String phone, final String password) {
        User user = new User();
        user.setPassword(password);
        int rows = userMapper.update(user, Wrappers.<User>lambdaUpdate().eq(User::getPhone, phone));
        return rows > 0;
    }

    /**
     * 获取分页用户数据
     *
     * @param page
     * @param user
     * @return
     */
    public Page<User> selectPage(Page page, User user) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(user.getName())) {
            wrapper = wrapper.like(User::getName, user.getName());
        }
        if (user.getStatus() != null) {
            wrapper = wrapper.eq(User::getStatus, user.getStatus());
        }
        return userMapper.selectPage(page, wrapper);
    }

    /**
     * 查询用户
     *
     * @param user
     * @return
     */
    public User queryUser(User user) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        if (user.getId() != null) {
            wrapper = wrapper.eq(User::getId, user.getId());
        }
        if (user.getWechatId() != null) {
            wrapper = wrapper.eq(User::getWechatId, user.getWechatId());
        }
        if (user.getName() != null) {
            wrapper = wrapper.eq(User::getName, user.getName());
        }
        if (user.getPositionId() != null) {
            wrapper = wrapper.eq(User::getPositionId, user.getPositionId());
        }
        return userMapper.selectOne(wrapper);
    }

    /**
     * 刷新用户等级
     *
     * @param user
     * @return
     */
    public float refreshLevel(User user) {
        return 1;
    }

    /**
     * 根据推广位查询用户
     *
     * @param positionId
     * @return
     */
    public User getUserByPositionId(Long positionId) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper = queryWrapper.eq(User::getPositionId, positionId);
        return userMapper.selectOne(queryWrapper);
    }
}
